package com.mini.utils;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class GlobalConstants {

	private static Boolean isDevMode=null;
	
	private static Properties config;

    private static final Logger log = Logger.getLogger(GlobalConstants.class);
	
    public static boolean isDevMode() {
    	if(isDevMode==null) {
    		init();
    	}
    	return isDevMode;
    }



	public synchronized static void init() {

		if (GlobalConstants.config != null) {
			return;
		}

		GlobalConstants.config = new Properties();
		
		loadPropFile("global.properties");


		Configure.setAppID(GlobalConstants.config.get("APPID").toString());

		return;
	}

	private static void loadPropFile(String file) {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		try (InputStream in = cl.getResourceAsStream(file);InputStreamReader reader = new InputStreamReader(in, "UTF-8");) {
			Properties props = new Properties();
			props.load(reader);
			for (Object key : props.keySet()) {
				GlobalConstants.config.put(key, props.get(key).toString().trim());
			}
		} catch (IOException e) {
			log.error("读配置错误：",e);;
		}
	}

	
    /**
     *
     */
    public static String get(String key) {
    	if(config==null) {
    		init();
    	}
        return (String) config.get(key);
    }


    /**
     * 获取缓存的电池信息
     *
     * @param key
     * @return
     */

    public static Object getObj(String key) {
    	if(config==null) {
    		init();
    	}
        return config.get(key);
    }

    public static void put(String key,String value) {
    	if(config==null) {
    		init();
    	}
        config.put(key,value);
    }
}
